<!--商铺管理-优惠券管理 -->
<template>
  <div class="admin-body">
    <el-row>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item >优惠券管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
    <el-row style="padding: 15px">

      <el-row class="main">
        <el-row  style="min-height: 40px;display: flex;">
          <el-col  style="margin-bottom: 5px;" align="left">
            <el-input v-model="key" clearable style="width: 300px;" placeholder="输入关键词查询" class="kz_search">
              <el-button slot="append" type="primary"  @click="getstart()">搜索</el-button>
            </el-input>
          </el-col>
          <el-col  style="margin-bottom: 5px;" align="right">
            <el-button type="primary" @click="classified()" style="">优惠券类型</el-button>
            <el-button type="primary" @click="add()" style="">添加优惠券</el-button>
          </el-col>
        </el-row>
        <!--表格-->
        <el-table
          select-on-indeterminate
          ref="multipleTable"
          border
          align="left"
          :data="tableData">
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
            prop="type_name"
            label="优惠券类型">
          </el-table-column>
          <el-table-column
            prop="intro"
            label="优惠券内容">
          </el-table-column>
          <el-table-column
            label="有效日期">
            <template slot-scope="scope">
              {{formatDate(scope.row.start_time)}}-{{formatDate(scope.row.over_time)}}
            </template>
          </el-table-column>
          <el-table-column
            prop="rule"
            label="使用规则">
            <template slot-scope="scope">
              满{{scope.row.rule}}可用
            </template>
          </el-table-column>
          <el-table-column
            label="操作">
            <template slot-scope="scope">
              <el-button @click="changeMes(scope.row.id,0)" style="color: red" size="small" type="text">
                删除
              </el-button>
              <el-button @click="edit(scope.row)"  style="" size="small" type="text">
                编辑
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <el-row style="margin-top: 20px">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" align="right">
            <el-pagination
              style="text-align: right;margin-bottom:70px;"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="dataTotal">
            </el-pagination>
          </el-col>
        </el-row>
      </el-row>
    </el-row>
    <el-dialog title="添加优惠券" :visible.sync="addVisible" width="500px" :modal-append-to-body="false">
      <el-form :model="adddata" :rules="addrules" ref="adddata">
        <el-form-item label="优惠券简介：" :label-width="formLabelWidth" prop="intro">
          <el-input v-model="adddata.intro" clearable></el-input>
        </el-form-item>
        <el-form-item label="优惠券抵扣金额：" :label-width="formLabelWidth" prop="deduction_money">
          <el-input  v-model="adddata.deduction_money"  clearable></el-input>
        </el-form-item>
        <el-form-item label="优惠券类型：" :label-width="formLabelWidth" prop="type_id">
          <el-select style="margin-left: 20px" v-model="adddata.type_id" clearable  placeholder="请选择优惠券类型">
            <el-option
              v-for="item in lxData"
              :key="item.id"
              :label="item.type_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效日期：" :label-width="formLabelWidth">
          <div  style="display: flex">
            <el-date-picker
              v-model="adddata.start_time"
              type="month"
              placeholder="选择开始月"
              style="width: 150px;margin-right: 10px">
            </el-date-picker>
            ~
            <el-date-picker
              v-model="adddata.over_time"
              type="month"
              placeholder="选择结束月"
              style="width: 150px;margin-left: 10px">
            </el-date-picker>
          </div>

        </el-form-item>
        <el-form-item label="使用规则：" :label-width="formLabelWidth" prop="rule">
          <div style="display: flex;">
            满
            <el-input v-model="adddata.rule" type="number" style="width: 200px;margin: 0 10px 0 10px" clearable></el-input>可用
          </div>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="SubmitAdd('adddata')">提交审核</el-button>
        <el-button @click="addVisible = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑优惠券" :visible.sync="editVisible" width="500px" :modal-append-to-body="false">
      <el-form :model="editdata" :rules="editrules" ref="editdata">
        <el-form-item label="优惠券简介：" :label-width="formLabelWidth" prop="intro">
          <el-input v-model="editdata.intro" clearable></el-input>
        </el-form-item>
        <el-form-item label="优惠券抵扣金额：" :label-width="formLabelWidth" prop="deduction_money">
          <el-input  v-model="editdata.deduction_money"  clearable></el-input>
        </el-form-item>
        <el-form-item label="优惠券类型：" :label-width="formLabelWidth" prop="type_id">
          <el-select style="margin-left: 20px" v-model="editdata.type_id" clearable  placeholder="请选择优惠券类型">
            <el-option
              v-for="item in lxData"
              :key="item.id"
              :label="item.type_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效日期：" :label-width="formLabelWidth" prop="start_time over_time">
          <div  style="display: flex">
            <el-date-picker
              v-model="editdata.start_time"
              type="month"
              placeholder="选择开始月"
              style="width: 150px;margin-right: 10px">
            </el-date-picker>
            ~
            <el-date-picker
              v-model="editdata.over_time"
              type="month"
              placeholder="选择结束月"
              style="width: 150px;margin-left: 10px">
            </el-date-picker>
          </div>

        </el-form-item>
        <el-form-item label="使用规则：" :label-width="formLabelWidth" prop="rule">
          <el-input v-model="editdata.rule"   clearable></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="SubmitEdit('editdata')">提交审核</el-button>
        <el-button @click="editVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "CouponManage",
    mounted() {
      // if (!this.$store.state.login.in) {
      //   this.$router.push({name: "login", params: {userId: 123}})
      // }
      this.getstart()
    },
    data() {
      return {
        pageSize: 10,
        page: 1,
        dataTotal: 0,
        tableData:[],
        addVisible:false,
        formLabelWidth:'140px',
        adddata:{
          intro:'',
          shop_id:'',
          rule:'',
          type_id:'',
          start_time:'',
          over_time:'',
          deduction_money:'',
        },
        lxData:[],
        rangedate:'',
        addrules:{
          intro: [
            {required: true, message: '请输入优惠券内容', trigger: 'blur'}
          ],
          rule: [
            {required: true, message: '请输入优惠券使用规则', trigger: 'blur'}
          ],
          start_time: [
            {required: true, message: '请选择开始时间', trigger: 'blur'}
          ],
          over_time: [
            {required: true, message: '请选择结束时间', trigger: 'blur'}
          ],
          type_id: [
            {required: true, message: '请选择优惠券类型', trigger: 'blur'}
          ],
          deduction_money: [
            {required: true, message: '请输入抵扣金额', trigger: 'blur'}
          ],
        },
        editVisible:false,
        editdata:{
          id:'',
          intro:'',
          rule:'',
          type_id:'',
          start_time:'',
          over_time:'',
          deduction_money:'',
        },
        editrules:{
          intro: [
            {required: true, message: '请输入优惠券内容', trigger: 'blur'}
          ],
          rule: [
            {required: true, message: '请输入优惠券使用规则', trigger: 'blur'}
          ],
          start_time: [
            {required: true, message: '请选择开始时间', trigger: 'blur'}
          ],
          over_time: [
            {required: true, message: '请选择结束时间', trigger: 'blur'}
          ],
          type_id: [
            {required: true, message: '请选择优惠券类型', trigger: 'blur'}
          ],
          deduction_money: [
            {required: true, message: '请输入抵扣金额', trigger: 'blur'}
          ],
        },
        key:'',
      }
    },
    computed:{
      userdata(){
        return this.$store.state.login.user
      }
    },
    methods: {
      classified(){
        this.$router.push({name:'couponClassified'})
      },
      getClassfied() {
        let thi = this
        const prams = {
          page:thi.page,
          pageSize:9999,
          key:thi.key,
          shop_id:thi.$store.state.login.user.id,
        }
        thi.mySelectAll('couponTypeList',prams).then(function (value) {
          thi.lxData = value.data
        })
      },
      edit(data) {
        console.log(data)
        this.editdata = JSON.parse(JSON.stringify(data))
        this.editdata.start_time = this.editdata.start_time * 1000
        this.editdata.over_time = this.editdata.over_time * 1000
        this.editdata.id = this.editdata.id
        console.log(this.editdata.start_time)
        console.log(this.editdata.over_time)
        this.getClassfied()
        this.editVisible = true
      },
      SubmitEdit(formName){
        let thi = this
        thi.editdata.start_time = Math.round(thi.editdata.start_time)/1000
        thi.editdata.over_time = Math.round(thi.editdata.over_time)/1000
        thi.$refs[formName].validate((valid) => {
          if (valid) {
            thi.mySubmit('editCoupon', thi.editdata).then(function (code) {
              if (code==200) {
                thi.editVisible = false
                thi.getstart()
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      SubmitAdd(formName){
        let thi = this
        thi.adddata.shop_id = thi.$store.state.login.user.id
        thi.adddata.start_time = Math.round(thi.adddata.start_time)/1000
        thi.adddata.over_time = Math.round(thi.adddata.over_time)/1000
        thi.$refs[formName].validate((valid) => {
          if (valid) {
            thi.mySubmit('addCoupon', thi.adddata).then(function (code) {
              if (code==200) {
                thi.addVisible = false
                thi.getstart()
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      add(){
        this.getClassfied()
        this.addVisible = true
      },
      //绑定表格数据，分页
      getstart() {
        let thi = this
        const prams = {
          page:thi.page,
          pageSize:thi.pageSize,
          key:thi.key,
          shop_id:thi.$store.state.login.user.id,
        }
        thi.mySelectAll('couponList',prams).then(function (value) {
          thi.tableData = value.data
          thi.dataTotal = value.Total
        })
      },

      //选择展示数量
      handleSizeChange(val) {
        this.pageSize = val
        this.getstart()
      },
      //分页
      handleCurrentChange(val) {
        this.page = val
        this.getstart()
      },
      //状态改变提示
      changeMes(id, state) {
        var heeh = '该'
        var caozuo = '禁用'
        if (id.length > 1) {
          heeh = '这些'
        }
        if (state == 0) {
          caozuo = '删除'
        }
        let thi = this
        thi.$confirm('此操作将'+caozuo + heeh+'优惠券', '是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).then(() => {
          thi.submitState(id,state)
        }).catch(() => {
          thi.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      //删除
      submitState(id,state) {
        let thi = this
        const por = {
          id: id,
          state: state,
        }
        console.log(por)
        thi.mySubmit('editCouponState', por).then(function (code) {
          if (code == 200) {
            thi.getstart()
          }
        })
      },
      formatDate(date) {
        if (!date) {
          return "无"
        }
        Date.prototype.toLocaleString = function () {
          return this.getFullYear() + "." + (this.getMonth() + 1)
        };
        var unixTimestamp = new Date(date * 1000)
        return unixTimestamp.toLocaleString();
      }
    }
  }

</script>

<style scoped>
  .main {
    background-color: white;
    min-height: 500px;
    padding: 10px;
    border: #EDEDED 1px solid;
    border-radius: 5px;
  }
  .el-row{
    /*防止页面抖动*/
    width: calc(100% - 2px) !important;
  }
</style>
